package com.xbs.dao.hitchhiking;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.ParamSet;
import com.xbs.dao.conn.RowDatas;
import com.xbs.entity.EmployeeEntity;

/**
 * 后台员工管理dao层
 * @author dell
 */
public class EmployeeAdminDao extends BaseDao{

	/**
	 * 实例化一个员工对象
	 */
	private RowDatas<EmployeeEntity> employee;

	//构造函数初始化
	public EmployeeAdminDao() {
		employee = new EmployeeData();
		}

	
	/**
	 * 1、查询所有员工
	 */
	public List<EmployeeEntity> getAllEmployee(){
		String sql = "select * from tb_employee where emp_status = '在职'";
		return queryList(sql, employee);
	}
	
	/**
	 * 2.向员工表中插入一条数据
	 */
	public int insertEmployee(EmployeeEntity emp){
		int row = 0;
		String sql = "insert into tb_employee(emp_name,emp_sex,"
				+ " emp_role,emp_tel,emp_store,emp_idcard,emp_status,"
				+ " emp_input)  values(?,?,?,?,?,?,?,?)";
		row = updateExecuted(sql, new ParamSet(emp.getEmpname(),emp.getEmpsex(),emp.getEmprole(),emp.getEmptel(),emp.getEmpstore(),emp.getEmpidcard(),emp.getEmpstatus(),emp.getEmpinput()));
		return row;
	}	
	
	/**
	 * 3.根据身份证来获取员工信息，并且进行修改信息
	 */
	public List<EmployeeEntity> updateEmpByid(String empid){
		String sql = "select * from tb_employee where emp_idcard = ?";
		return queryList(sql, new ParamSet(empid),employee);
	}
	
	/**
	 * 4.更改数据库员工表中的数据
	 */
	public int updateEmp(EmployeeEntity emp){
		int row = 0;
		String sql = "update tb_employee set emp_name=?,emp_sex=?,"
				+ " emp_role=?,emp_tel=?,emp_store=?,emp_idcard=?,"
				+ " emp_status=?,emp_input=? where emp_idcard=?";
		System.out.println(sql);
		row = updateExecuted(sql, new ParamSet(emp.getEmpname(),emp.getEmpsex(),
				emp.getEmprole(),emp.getEmptel(),emp.getEmpstore(),emp.getEmpidcard(),
				emp.getEmpstatus(),emp.getEmpinput(),emp.getEmpidcard()));
		return row;
	}
	
	/**
	 * 5.根据姓名，身份证，编号进行查询员工
	 */
	public List<EmployeeEntity> getEmployeeByThree(String str){
		String sql = "select * from tb_employee where emp_status = '在职' "
				+ " and (emp_name like '%"+str+"%' or emp_idcard like '%"+str+"%' "
				+ " or emp_id like '%"+str+"%')";
		return queryList(sql, employee);
		
	}
	
	/**
	 * 内部类实现接口，员工对象
	 * @author dell
	 */
	class EmployeeData implements RowDatas<EmployeeEntity>{

		@Override
		public EmployeeEntity getRowData(ResultSet rs) throws SQLException {
			EmployeeEntity employee = new EmployeeEntity();
			employee.setEmpid(rs.getInt("emp_id"));
			employee.setEmpidcard(rs.getString("emp_idcard"));
			employee.setEmpinput(rs.getString("emp_input"));
			employee.setEmpname(rs.getString("emp_name"));
			employee.setEmprole(rs.getString("emp_role"));
			employee.setEmpsex(rs.getString("emp_sex"));
			employee.setEmpstatus(rs.getString("emp_status"));
			employee.setEmpstore(rs.getString("emp_store"));
			employee.setEmptel(rs.getString("emp_tel"));
			return employee;
		}
		
	}
	

	
}
